package com.mifengs.mall.widget;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.TabBean;
import com.mifengs.mall.util.UIUtil;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import java.util.ArrayList;

/**
 * Created by lihaoqi on 2018/4/17.
 */

public class MyTabProvider implements SmartTabLayout.TabProvider{

    private ArrayList<TabBean> tabBeanList;

    private Context context;

    public MyTabProvider(ArrayList<TabBean> tabBeanList, Context context) {
       this.tabBeanList = tabBeanList;
       this.context = context;
    }

   private ArrayList<Integer> mImagePosition = new ArrayList<>();

    @Override
    public View createTabView(ViewGroup viewGroup, int position, PagerAdapter pagerAdapter) {

        final LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        final View tabView = inflater.inflate(R.layout.tab_view,viewGroup,
                false);
        TextView tvTitle = tabView.findViewById(R.id.title);
        final ImageView ivTitle = tabView.findViewById(R.id.iv_title);
        TabBean bean = tabBeanList.get(position);
        if(TextUtils.isEmpty(bean.getImg())||position==0){
            tvTitle.setText(bean.getTitle());
            ivTitle.setVisibility(View.GONE);
            tvTitle.setVisibility(View.VISIBLE);
        }else{
            if(!mImagePosition.contains(position)){
                mImagePosition.add(position);
            }
            ivTitle.setVisibility(View.VISIBLE);
            tvTitle.setVisibility(View.GONE);
            //ImageLoadUtil.displayImage(context,bean.getImg(),ivTitle);
            String photoUrl = PreferenceUtils.getPhotoPath(context);
            Glide.with(context.getApplicationContext())
                    .load(photoUrl+bean.getImg())
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            if(resource instanceof GlideBitmapDrawable){
                                GlideBitmapDrawable drawable = (GlideBitmapDrawable) resource;
                                if (ivTitle == null) {
                                    return false;
                                }
                                if (ivTitle.getScaleType() != ImageView.ScaleType.FIT_XY) {
                                    ivTitle.setScaleType(ImageView.ScaleType.FIT_XY);
                                }
                                ViewGroup.LayoutParams params = ivTitle.getLayoutParams();
                                float scale = drawable.getIntrinsicWidth()/(float) drawable.getIntrinsicHeight();
                                int vh = UIUtil.dp2px(30);
                                int vw = (int) (vh*scale);
                                params.height = vh;
                                params.width = vw;
                                ivTitle.setLayoutParams(params);
                            }
                            return false;
                        }
                    })
                    .into(ivTitle);

        }
        return tabView;
    }

    public ArrayList<Integer> getmImagePosition() {
        return mImagePosition;
    }
}
